<?php

/**
 * Description of UtenteDataManager
 *
 * @author rocco
 */
require_once '../bean/Domanda.php';
require_once '../bean/Tests.php';
require_once '../bean/Studente.php';


class TestStudentiDataManager
{
    private static $instance;

    public static function getInstance()
    {
        if(self::$instance == null)
        {
            $c = __CLASS__;
            self::$instance = new $c;
        }
        return self::$instance;
    }

    public function insertNewArgomento($connection, $codiceDisciplina, $nome, $descrizione)
    {

        $query = "Insert into Argomento (nome, descrizione, codiceDisciplina) VALUES(?, ?, ?) ";
        $stmt = $connection->prepare($query);
        $stmt->bind_param( "ssi", $nome, $descrizione, $codiceDisciplina);
        $stmt->execute();
        $stmt->close();
    }



    public function updateArgomento($connection, $codice, $nome, $descrizione)
    {
        $query =  "Update Argomento set nome = ?, descrizione = ? where codice = ?";
        echo 'query ' .$query . ' nome :' . $nome;
        $stmt = $connection->prepare($query);
        $stmt->bind_param( "ssi", $nome, $descrizione, $codice);
        $stmt->execute();
        $stmt->close();
    }



    public function deleteArgomento($connection, $codice)
    {
        $query = "delete from Argomento where codice = ? ";
        $stmt = $connection->prepare($query);
        $stmt->bind_param( "i", $codice);
        $stmt->execute();
        $stmt->close();
    }

    public function existTestForStudent($connection, $codiceTest, $chiaveTest, $codiceStudente)
    {
        $query = sprintf("Select codiceTest as codice from TestStudenti where attivo=1 and codiceTest = %d AND chiaveTest = %d AND codiceStudente = %d", $codiceTest, $chiaveTest, $codiceStudente);
        if($stmt = $connection->prepare($query))
            $stmt->execute();

        $stmt->bind_result($codice);

        $res = FALSE;

        if ($stmt->fetch())
        {
            $res = TRUE;
        }
        $stmt->close();
        return $res;
    }

    public function getListaDomandeStudente($connection, $codiceStudente, $codiceTest, $chiaveTest)
    {
        $query = sprintf("Select codiceDomanda from TestStudentiRisultati where codiceTest = %d AND codiceStudente = %d and chiaveTest = '".$chiaveTest."'", $codiceTest, $codiceStudente);
        if($stmt = $connection->prepare($query))
            $stmt->execute();

        $stmt->bind_result($codiceDomanda);

        $lista = array();

        while ($stmt->fetch())
        {
            $lista[]=$codiceDomanda;
        }
        $stmt->close();
        return $lista;
    }

    public function getSeedQuestion($connection, $codiceStudente, $codiceTest, $chiaveTest)
    {
        $query = sprintf("Select seedQuestion from TestStudenti where codiceTest = %d AND codiceStudente = %d and chiaveTest = '".$chiaveTest."'", $codiceTest, $codiceStudente);
        if($stmt = $connection->prepare($query))
            $stmt->execute();

        $stmt->bind_result($seedQuestion);

        $result="";

        if ($stmt->fetch())
        {
            $result=$seedQuestion;
        }
        $stmt->close();
        return $result;
    }
}
?>
